<template>
  <div class="MenuPage">
    <el-menu
      background-color="#333744" text-color="#fff" active-text-color="#409BFF"
      router
      :default-active="activepath"
      class="el-menu-vertical-demo"
      @select="handleSelect"
      :collapse="isCollapse"
      :unique-opened="true"
      v-for="(item, index) in menus"
      :key="index"
    >
      <template v-if="item.children.length > 0">
        <el-submenu :index="item.path" v-if="item.show">
          <template slot="title">
            <i :class="item.fontawesome"></i>
            <span> {{ item.name }} </span>
          </template>
          <el-menu-item-group
            v-for="(child, inde) in item.children"
            :key="inde"
          >
            <el-menu-item :index="child.path" v-if="child.show">
              <i :class="child.fontawesome"></i>
              <span> {{ child.name }} </span>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </template>
      <template v-else>
        <el-menu-item :index="item.path">
          <i :class="item.fontawesome"></i>
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script>
import Enumerable from 'linq'
export default {
  name: 'MenuPage',
  data () {
    return {
      menus: [],
      activepath: '',
      isCollapse: false
    }
  },
  created () {
    this.menus = [...this.$router.options.routes]
    this.menus = Enumerable.from(this.menus).where(p => p.show === true).toArray()
    this.activepath = this.$router.currentRoute.path
  },
  methods: {
    // handleOpen (key, keyPath) {
    //   console.log(key, keyPath)
    // },
    // handleClose (key, keyPath) {
    //   console.log(key, keyPath)
    // },
    handleSelect (key, keyPath) {
      this.$emit('onclick', key, keyPath)
    }
  },
  mounted () {
    this.$bus.$on('changeEvent', (res) => {
      this.isCollapse = res.a
    })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
.el-menu {
  color: chartreuse;
  text-align: left;
  border: 0px;
}
::v-deep .el-menu-item-group__title{
  padding: 0;
}
</style>
